<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>私聊</title>
    <style>
        *, *:before, *:after {
            box-sizing: border-box;
        }
        body, html {
            height: 100%;
            overflow: hidden;
        }
        body, ul {
            margin: 0;
            padding: 0;
        }
        body {
            color: #4d4d4d;
            font: 14px/1.4em 'Helvetica Neue', Helvetica, 'Microsoft Yahei', Arial, sans-serif;
            background: #f5f5f5 url('dist/images/bg.jpg') no-repeat center;
            background-size: cover;
            font-smoothing: antialiased;
        }
        ul {
            list-style: none;
        }
        #chat {
            margin: 20px auto;
            width: 800px;
        	height: 600px;
        	overflow: hidden;
        	border-radius: 3px;
        }
        
        #chat .sidebar, .main{
        	height: 100%;
        }
        
        #chat .sidebar{
        	float: left;
            width: 200px;
            color: #f4f4f4;
            background-color: #2e3238;
        }
        
        #chat .main{
        	position: relative;
            overflow: hidden;   
            background-color: #eee;
        }
        
       #chat .main .m-text {
            position: absolute;
            width: 100%;
            bottom: 0;
            left: 0;
            background-color: #fff;
        }
       #chat .m-message {
            height: calc(100% - 10pc);
        }
       #chat .sidebar .m-card{
	       	padding: 12px;
	        border-bottom: solid 1px #24272C;
       }
       
       #chat .sidebar .m-card footer {
            margin-top: 10px;
        }
        
        #chat .sidebar .avatar, .name {
            vertical-align: middle;
        }
        #chat .sidebar .avatar {
            border-radius: 2px;
        }
        #chat .sidebar .name {
            display: inline-block;
            margin: 0 0 0 15px;
            font-size: 16px;
        }
         #chat .sidebar .search {
            padding: 0 10px;
            width: 100%;
            font-size: 12px;
            color: #fff;
            height: 30px;
            line-height: 30px;
            border: solid 1px #3a3a3a;
            border-radius: 4px;
            outline: none;
            background-color: #26292E;
        }
        
        .m-list li {
            padding: 12px 15px;
            border-bottom: 1px solid #292C33;
            cursor: pointer;
            transition: background-color .1s;
        }
        .m-list li:hover {
			 background-color: rgba(255, 255, 255, 0.03);
		}
       	.m-list li:active {
			 background-color: rgba(255, 255, 255, 0.1);
		}
		.m-list .avatar, .name {
            vertical-align: middle;
        }
        .m-list .avatar {
            border-radius: 2px;
        }
        .m-list .name {
            display: inline-block;
            margin: 0 0 0 15px;
        }
        
        
        .m-message {
	        padding: 10px 15px;
	        overflow-y: scroll;
        }
        
        .m-message li {
            margin-bottom: 15px;
        }
        
        .m-message .time {
            margin: 7px 0;
            text-align: center;
        }
        
        .m-message .time> span {
             display: inline-block;
             padding: 0 18px;
             font-size: 12px;
             color: #fff;
             border-radius: 2px;
             background-color: #dcdcdc;
         }
         
        .m-message .avatar {
            float: left;
            margin: 0 10px 0 0;
            border-radius: 3px;
        }
       	.m-message .text {
            display: inline-block;
            position: relative;
            padding: 0 10px;
            max-width: ~'calc(100% - 40px)';
           	min-height:30px;
            line-height: 2.5;
            font-size: 12px;
            text-align: left;
            word-break: break-all;
            background-color: #fafafa;
            border-radius: 4px;
        }
        .m-message .text:before{
        	content: " ";
            position: absolute;
            top: 9px;
            right: 100%;
            border: 6px solid transparent;
            border-right-color: #fafafa;
        }
        .m-message .self {
            text-align: right;
        }
        
        .m-message .self .avatar {
              float: right;
              margin: 0 0 0 10px;
        }
        
        .m-message .self .text {
           background-color: #b2e281;
         }
         .m-message .self .text:before {
             right: inherit;
             left: 100%;
             border-right-color: transparent;
             border-left-color: #b2e281;
         }
      	
        .m-text {
	        height: 160px;
	        border-top: solid 1px #ddd;
    	}
    	
    	.m-text textarea {
            padding: 10px;
            height: 100%;
            width: 90%;
            border: none;
            outline: none;
            font-family: "Micrsofot Yahei";
            resize: none;
        }
        .m-text button{
        	height: 40px;
        	margin-top: 100px;
        	position: absolute;
        	border-radius: 5px;
        	background-color: #2e38380f;
        }
    </style>
<!-- 引入Vue -->
<script src="../js/vue.min.js"></script>
<!-- 引入axios -->
<script type="text/javascript"
	src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.0/axios.min.js"></script>
</head>
<body>

	<div id="chat">
		<div class="sidebar">
			<div class="m-card">
				<header>
				<img class="avatar" alt="Coffce" :src="loginedUser.image" width="40"
					height="40"><p class="name">{{loginedUser.name}}</p></header>
				<footer>
				<input class="search" placeholder="search friend..."></footer>
			</div>
			<!--v-component-->
			<div class="m-list">
				<ul>
					<!--v-for-start-->
					<li><img class="avatar" :alt="recvUser.name" :src="recvUser.image"
						width="30" height="30"><p class="name">{{recvUser.name}}</p></li>
					<!--v-for-end-->
				</ul>
			</div>
			<!--v-component-->
		</div>
		<div class="main">
			<div id = "scrolldIV" class="m-message" style="overflow:auto;">
				<ul>
					<!--v-for-start-->
					<li v-for="(m,i) in messages"><p class="time">
							<span>{{m.sendTime}}</span>
						</p>
						<div :class="m.sendUid==loginedUser.id ? 'main self' : 'main'">
							<img class="avatar" :src="m.sendUid==loginedUser.id ? loginedUser.image : recvUser.image" width="30"
								height="30"><div class="text">{{m.content}}</div>
						</div></li>
					<!--v-for-end-->
				</ul>
			</div>
			<!--v-component-->
			<div class="m-text">
				<textarea placeholder="" v-model="content"></textarea>
				<button @click="send">发送</button>
			</div>
			<!--v-component-->
		</div>
	</div>
	<script>
	/*让滚动条保持在底部*/
	function divBottom()
	{
		var div = document.getElementById('scrolldIV');
		div.scrollTop = div.scrollHeight - div.clientHeight;
	}
	/*获取当前时间*/
	function getTime(){
		 var myDate = new Date();
	        //获取当前年
	        var year=myDate.getFullYear();
	        //获取当前月
	        var month=myDate.getMonth()+1;
	        //获取当前日
	        var date=myDate.getDate();
	        var h=myDate.getHours();       //获取当前小时数(0-23)
	        var m=myDate.getMinutes();     //获取当前分钟数(0-59)
	        if(m<10) m = '0' + m;
	        var s=myDate.getSeconds();
	        if(s<10) s = '0' + s;
	        var now=year+'-'+month+"-"+date+" "+h+':'+m+":"+s;
		return now;
	}
		
	var v = new Vue({
		el : "#chat",
		data : {
			recvUid : "",//接受者id,从url地址中获取
			recvUser : {},//根据recvUid查询到用户对象
			content : "",
			loginedUser : {},//当前用户
			messages : [], //消息
			count : 0,
		},
		created(){
			this.recvUid = location.href.replace(/^.*?id=(\d+)$/,"$1");
			let params = new URLSearchParams();
			params.append("recvUid",this.recvUid);
			axios.post("../get/loginedUser.s").then(res=>{
				this.loginedUser = res.data;
			});
			axios.post("../msg/getRecvUser.s",params).then(res=>{
				this.recvUser = res.data;
			});
			
			//启动定时任务
			window.setInterval(()=>{
				this.query();
			},1000);
		},
		methods : {
			send(){
				let params = new URLSearchParams();
				params.append("sendUid",this.loginedUser.id);
				params.append("recvUid",this.recvUid);
				params.append("content",this.content);
				params.append("sendTime",getTime());
				axios.post("../msg/send.s",params).then(res=>{
					if(res.data.code==0){
						alert(res.data.msg);
					}else{
						this.messages.push(res.data.data);
						this.content = "";
					}
					this.count = 0;
				});
			},
			query(){
				let params = new URLSearchParams();
				params.append("sendUid",this.loginedUser.id);
				params.append("recvUid",this.recvUid);
				axios.post("../msg/showMsg.s",params).then(res=>{
					this.messages = res.data;
				})
				//这里是让用户看到最新消息
				if(this.count==0){
					divBottom();
				}
				if(this.count==1){
					divBottom();
				}
				this.count++;
			}
		}
		
	});
	</script>
</body>
</html>
